<template>
  <div id="staDrugTestMthSeDiv">
    <Splitpanes style="height: 100%">
      <Pane size="50">
        <vxe-toolbar perfect>
          <template #buttons>
            <vxe-input style="width: 180px" v-model="searchForm.searchValue" type="search" clearable class="vxeInputSearch" placeholder="关键字查询" @clear="getStaDrugTestMthList" @keyup="keyupEnter" @search-click="getStaDrugTestMthList()"></vxe-input>
            <vxe-button type="text" class="button-blue" icon="vxe-icon-refresh" content="刷新" :loading="tloading" @click="getStaDrugTestMthList"></vxe-button>
            <vxe-button type="text" class="button-blue" icon="vxe-icon-square-plus" content="新建" @click="addStaDrugTestMthDialog()"></vxe-button>
            <vxe-button type="text" icon="vxe-icon-delete" class="button-red" content="删除" @click="deleteStaDrugTestMth"></vxe-button>
            <!-- <vxe-button type="text" class="button-blue" icon="vxe-icon-refresh" content="同步" :loading="tloading" @click="sysStaDrugTestMth"></vxe-button> -->

          </template>
        </vxe-toolbar>

        <div class="modulTable">
          <vxe-table ref="staDrugTestMthTableRef" class="mytable-scrollbar staDrugTestMthTableClazz" border="inner" align="center" height="auto" :data="staDrugTestMthList" :loading="tloading" @current-change="DrugTestMthRowClick" :row-config="{ isCurrent: true, isHover: true }" :column-config="{ resizable: true }" resizable show-overflow keep-source>
            <vxe-column type='checkbox' width='30px'></vxe-column>
            <vxe-column field='drugMthNo' title='编码'></vxe-column>
            <vxe-column field='drugMthNa' title='名称'></vxe-column>
            <vxe-column field='standardNa' title='标准名'></vxe-column>
            <vxe-column field='inputCode' title='输入码'></vxe-column>
            <vxe-column field='minMeasurement' title='最小测量值'></vxe-column>
            <vxe-column field='maxMeasurement' title='最大测量值'></vxe-column>
            <vxe-column field='isStatic' title='是否静态'>
              <template #default='{ row }'>
                <el-tag type='danger' v-if="row.isStatic === '0'">否</el-tag>
                <el-tag type='success' v-else-if="row.isStatic === '1'">是</el-tag>
                <span v-else>——</span>
              </template>
            </vxe-column>

            <vxe-column field='seq' title='显示顺序'></vxe-column>
          </vxe-table>
          <vxe-pager align="left" min-height="32px" :current-page.sync="page.currentPage" :page-size.sync="page.pageSize" :total="page.totalResult" :layouts="[
                'PrevJump',
                'PrevPage',
                'Number',
                'NextPage',
                'NextJump',
                'Sizes',
                'FullJump',
                'Total'
              ]" @page-change="handleSizeChange($event)"></vxe-pager>
        </div>
      </Pane>
      <Pane size="50">
        <el-tabs type="border-card" v-model="activeName" @tab-click="tabHandleClick">
          <el-tab-pane label="基础信息" name="first">
            <vxe-toolbar perfect>
              <template #buttons>
                <vxe-button type="text" class="button-blue" @click="saveStaDrugTestMth('staDrugTestMthFormRef')" icon="vxe-icon-square-plus" content="保存"></vxe-button>
              </template>
            </vxe-toolbar>
            <el-form ref="staDrugTestMthFormRef" :model="staDrugTestMthForm" :rules="staDrugTestMthFormRule" class="form-input-m" label-width="100px" inline-message>
              <el-row :gutter='20'>
                <el-col :span='12'>
                  <el-form-item label='编码' prop='drugMthNo'>
                    <el-input v-model='staDrugTestMthForm.drugMthNo' placeholder='请输入编码' clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span='12'>
                  <el-form-item label='名称' prop='drugMthNa'>
                    <el-input v-model='staDrugTestMthForm.drugMthNa' placeholder='请输入名称' clearable @blur="addInputCode()"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter='20'>
                <el-col :span='12'>
                  <el-form-item label='最小测量值' prop='minMeasurement'>
                    <el-input v-model='staDrugTestMthForm.minMeasurement' placeholder='请输入最小测量值' type='number' clearable></el-input>
                  </el-form-item>
                </el-col>

                <el-col :span='12'>
                  <el-form-item label='最大测量值' prop='maxMeasurement'>
                    <el-input v-model='staDrugTestMthForm.maxMeasurement' placeholder='请输入最大测量值' type='number' clearable></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter='20'>
                <el-col :span='12'>
                  <el-form-item label='标准名' prop='standardNa'>
                    <el-input v-model='staDrugTestMthForm.standardNa' placeholder='请输入标准名' clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span='12'>
                  <el-form-item label='输入码' prop='inputCode'>
                    <el-input v-model='staDrugTestMthForm.inputCode' placeholder='请输入输入码' clearable></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter='20'>
                <el-col :span='12'>
                  <el-form-item label='是否静态' prop='isStatic'>
                    <el-switch v-model='staDrugTestMthForm.isStatic' inline-prompt active-text='是' inactive-text='否' active-value='1' inactive-value='0'></el-switch>
                  </el-form-item>
                </el-col>

                <el-col :span='12'>
                  <el-form-item label='显示顺序' prop='seq'>
                    <el-input v-model='staDrugTestMthForm.seq' type='text' placeholder='请输入显示顺序' clearable></el-input>
                  </el-form-item>
                </el-col>
              </el-row>

            </el-form>
          </el-tab-pane>
        </el-tabs>
      </Pane>
    </Splitpanes>
  </div>
</template>
<script setup lang="ts">
// 引入组件区 ==========
import { ref, reactive, getCurrentInstance, onMounted } from 'vue';
import { Splitpanes, Pane } from 'splitpanes';
import moment from 'moment';
let { proxy }: any = getCurrentInstance();
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage'; //提示
import { isEmpty } from 'lodash';
import { funBdsJsResult } from '@/utils/Expression'; //函数公式

defineExpose({ initPage });
let searchForm = {
  searchValue: ''
};
let tloading = ref(false); //加载状态
let activeName = ref('first');
let staDrugTestMthList: any = reactive([]);
let sltStaDrugTestMth: any = ref({});
// 条数/页
const PageSizeList = [10, 50, 100, 200, 500];
// 分页
let page = reactive({
  currentPage: 1,
  pageSize: PageSizeList[1],
  totalResult: 0
});

let staDrugTestMthForm = reactive({
  id: null,
  drugMthNo: '' /*编码*/,
  drugMthNa: '' /*名称*/,
  standardNa: '' /*标准名称*/,
  isStatic: '0' /*是否静态*/,
  inputCode: '',
  minMeasurement: 0 /*输入码*/,
  maxMeasurement: 1 /*输入码*/,
  seq: '' /*显示顺序*/
});
let visibleOutReporter: any = ref(false);

let staDrugTestMthFormRule = {
  instrName: [
    {
      required: true,
      message: '请输入仪器名称',
      trigger: 'blur'
    },
    {
      min: 2,
      max: 20,
      message: '长度为2到20个字符',
      trigger: 'blur'
    }
  ],
  instrCode: [
    {
      required: true,
      message: '请输入仪器编码',
      trigger: 'blur'
    }
  ],
  staInstrCode: [
    {
      required: true,
      message: '请输入标准编号',
      trigger: 'blur'
    }
  ]
};
onMounted(() => {
  //getDictItemsList();
});
function initPage() {
  getStaDrugTestMthList();
}

let instrClassDict = reactive([]); //仪器大类字典
let workGroupList = reactive([]); //仪器大类字典

//批量获取字典
async function getDictItemsList() {
  let dictTreeTypeCodes: any = ['YQDL', 'JYGZZ'];
  const codes = dictTreeTypeCodes.join(',');
  const url = '/api/sys/basDictDetail/findDictDetailListByDictCodes';
  const dictTreeTypeItems: any = await proxy.$http.get(url, { params: { code: codes } });
  const maintainTypes = dictTreeTypeItems.data.find((item) => item.code === 'YQDL');
  instrClassDict.push(...maintainTypes.detailDataList);
  const maintainStatues = dictTreeTypeItems.data.find((item) => item.code === 'JYGZZ');
  workGroupList.push(...maintainStatues.detailDataList);
}

function getStaDrugTestMthList() {
  tloading.value = true;
  staDrugTestMthList.length = 0; //放入数据前清空数据
  const url = '/lis/sta/staDrugTestMth/findStaDrugTestMthPageList';
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: {
        seachValue: searchForm.searchValue,
        pageIndex: page.currentPage,
        pageSize: page.pageSize
      }
    })
    .then((res: any) => {
      staDrugTestMthList.push(...res.data.records);
      page.totalResult = res.data.total;
      const kn = staDrugTestMthList.find((k) => k.id == sltStaDrugTestMth.value.id);
      if (isEmpty(kn)) {
        DrugTestMthRowClick({ row: staDrugTestMthList[0] }); //默认选中第一条数据
      } else {
        DrugTestMthRowClick({ row: kn }); //默认选中第一条数据
      }
      tloading.value = false;
    })
    .catch((err: any) => {
      tloading.value = false;
      // VxeMessage('获取获取当前字典的规则列表失败',VxeNotifyType.error)
    });
}

function sysStaDrugTestMth() {
  tloading.value = true;
  staDrugTestMthList.length = 0; //放入数据前清空数据
  const url = '/lis/sta/staDrugTestMth/sysStaDrugTestMth';
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: {
        seachValue: searchForm.searchValue,
        pageIndex: page.currentPage,
        pageSize: page.pageSize
      }
    })
    .then((res: any) => {
      staDrugTestMthList.push(...res.data.records);
      page.totalResult = res.data.total;
      const kn = staDrugTestMthList.find((k) => k.id == sltStaDrugTestMth.value.id);
      if (isEmpty(kn)) {
        DrugTestMthRowClick({ row: staDrugTestMthList[0] }); //默认选中第一条数据
      } else {
        DrugTestMthRowClick({ row: kn }); //默认选中第一条数据
      }
      tloading.value = false;
    })
    .catch((err: any) => {
      tloading.value = false;
      // VxeMessage('获取获取当前字典的规则列表失败',VxeNotifyType.error)
    });
}

//点击每页数量(分页查询)
function handleSizeChange({ type, currentPage, pageSize }) {
  if (type === 'size') {
    page.currentPage = 1;
  } else {
    page.currentPage = currentPage;
  }
  page.pageSize = pageSize;
  getStaDrugTestMthList();
}

function saveStaDrugTestMth(formName: any) {
  let $el = proxy.$refs[formName];
  $el.validate((valid: any) => {
    if (!valid) {
      return false;
    }
    if (staDrugTestMthForm.id === null) {
      const url = '/lis/sta/staDrugTestMth/addStaDrugTestMth';

      proxy.$http
        .post(url, staDrugTestMthForm)
        .then((res: any) => {
          clearForm();
          sltStaDrugTestMth.value.id = res.data.id;
          getStaDrugTestMthList();
        })
        .catch((err: any) => {
          VxeMessage('新增失败', VxeNotifyType.error);
        });
    } else {
      // 修改用户
      const url = '/lis/sta/staDrugTestMth/editStaDrugTestMth';

      proxy.$http
        .post(url, staDrugTestMthForm)
        .then((res: any) => {
          VxeMessage('修改成功', VxeNotifyType.success);
          sltStaDrugTestMth.value.id = res.data.id;
          setTimeout(() => {
            clearForm();
            getStaDrugTestMthList();
          }, 50);
        })
        .catch((err: any) => {
          VxeMessage('修改失败', VxeNotifyType.error);
        });
    }
  });
}
function deleteStaDrugTestMth() {
  const sltStaDrugTestMths = (proxy.$refs.staDrugTestMthTableRef as any).getCheckboxRecords();
  if (!(sltStaDrugTestMths.length > 0)) {
    VxeMessage('请勾选要删除的数据', VxeNotifyType.info);
  } else {
    proxy
      .$confirm('确定要删除当前勾选的数据？', '提示', {
        cancelButtonClass: 'cancel-button',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(async () => {
        //后台删除数据
        const delStaDrugTestMthList = sltStaDrugTestMths.filter((p: any) => !isEmpty(p.id));
        if (delStaDrugTestMthList.length > 0) {
          const data = { staDrugTestMthList: delStaDrugTestMthList };
          const url = '/lis/sta/staDrugTestMth/batchDeleteStaDrugTestMthList';
          proxy.$http
            .post(url, data)
            .then((res: any) => {
              getStaDrugTestMthList(); // 获取检验仪器表格数据
              VxeMessage(res.status.msg, VxeNotifyType.success);
            })
            .catch((err: any) => {
              VxeMessage(err, VxeNotifyType.error);
            });
        }
      })
      .catch((err: any) => {
        // VxeMessage(err, VxeNotifyType.error);
      });
  }
}
// 回车查询
function keyupEnter(e: any) {
  if (e.$event.code == 'Enter') {
    getStaDrugTestMthList();
  }
}
// 点击设计模式表格当前行触发右侧表单变化
function DrugTestMthRowClick({ row }: any) {
  if (isEmpty(row)) {
    clearForm();
    sltStaDrugTestMth.value = {};
  } else {
    (proxy.$refs.staDrugTestMthTableRef as any).setCurrentRow(row);
    setStaDrugTestMthForm(row);
  }
}

function addStaDrugTestMthDialog() {
  clearForm();
  sltStaDrugTestMth.value = {};
  (proxy.$refs.staDrugTestMthTableRef as any).clearCurrentRow();
  activeName.value = 'first';
}

// 清空中间规则弹框表单
function clearForm() {
  for (let key in staDrugTestMthForm) {
    staDrugTestMthForm[key] = '';
  }
  staDrugTestMthForm.id = null;
  staDrugTestMthForm.isStatic = '0';
}

// 编辑当前行规则
function setStaDrugTestMthForm(row: any) {
  if (row !== null) {
    for (let prop in row) {
      staDrugTestMthForm[prop] = row[prop];
    }
  }
  sltStaDrugTestMth.value = row;
}

function tabHandleClick(tab: any) {}

// 填写仪器名称 自动生成助记符
function addInputCode() {
  staDrugTestMthForm.inputCode = funBdsJsResult('firstPinYinFun', staDrugTestMthForm.name);
}
const predefineColors = ref(['#ff4500', '#ff8c00', '#ffd700', '#90ee90', '#00ced1', '#1e90ff', '#c71585', 'rgba(255, 69, 0, 0.68)', 'rgb(255, 120, 0)', 'hsv(51, 100, 98)', 'hsva(120, 40, 94, 0.5)', 'hsl(181, 100%, 37%)', 'hsla(209, 100%, 56%, 0.73)', '#c7158577']);
</script>

<style lang="scss" scoped>
#staDrugTestMthSeDiv {
  height: 100%;

  // 附加按钮间距
  .button-blue,
  .button-red {
    margin: 5px 0 5px 5px;
  }

  //右侧质控项目表格
  .modulTable {
    height: calc(100% - 40px - 30px);
    background-color: $base-white;
  }
}
</style>